<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <!-- 引入 lib 目录下的 bootstrap 样式表 -->
  <link rel="stylesheet" href="./lib/bootstrap-v4.6.0.css">
  <style>
    :root {
      font-size: 15px;
    }

    body {
      padding-top: 15px;
    }
  </style>
</head>

<body>

  <!-- 栅格系统 -->
  <div class="container-fluid">
    <!-- 栅格系统中的一行 -->
    <div class="row">
      <!-- 左侧的表格，占了 8 列 -->
      <div class="col-sm-8">
        <table class="table table-bordered table-striped table-dark table-hover text-center">
          <thead>
            <!-- 表头行 -->
            <tr>
              <th scope="col">Id</th>
              <th scope="col">书名</th>
              <th scope="col">作者</th>
              <th scope="col">出版社</th>
              <th scope="col">操作</th>
            </tr>
          </thead>
          <tbody>
            <!-- 表格中的每一行 -->

          </tbody>
        </table>
      </div>

      <!-- 右侧的添加区域，占了 4 列 -->
      <div class="col-sm-4">
        <!-- 添加图书的卡片 -->
        <div class="card text-white bg-secondary sticky-top">
          <div class="card-header">添加新图书</div>
          <form class="card-body bg-light" id="addForm">
            <!-- 书名 -->
            <div class="input-group mb-3">
              <div class="input-group-prepend">
                <span class="input-group-text">书名</span>
              </div>
              <input type="text" class="form-control" placeholder="请输入图书名称" name="bookname">
            </div>
            <!-- 作者 -->
            <div class="input-group mb-3">
              <div class="input-group-prepend">
                <span class="input-group-text">作者</span>
              </div>
              <input type="text" class="form-control" placeholder="请输入作者名字" name="author">
            </div>
            <!-- 出版社 -->
            <div class="input-group mb-3">
              <div class="input-group-prepend">
                <span class="input-group-text">出版社</span>
              </div>
              <input type="text" class="form-control" placeholder="请输入出版社名称" name="publisher">
            </div>
            <!-- 添加按钮 -->
            <button class="btn btn-dark" type="submit">添加</button>
          </form>
        </div>
      </div>
    </div>
  </div>

  <script src="./lib/axios.js"></script>
  <script src="./lib/form-serialize.js"></script>
  <script>

    //根路径
    axios.defaults.baseURL = 'http://www.liulongbin.top:3006'

    // 查元素
    let tbody = document.querySelector('tbody')
    let form  = document.querySelector('form')


    //获取数据
    function getData () {
      axios.get('/api/getbooks').then(({ data }) => {
        if(data.status == 200){ //业务状态码  后端的
          render(data.data)
        }
      })
    }

    // 封装函数渲染
    function render (data) {
      let str = ''  //存总体
      data.forEach((item) => {
        str += `
        <tr>
              <th scope="row">${item.id}</th>
              <td>${item.bookname}</td>
              <td>${item.author}</td>
              <td>${item.publisher}</td>
              <td>
                <button type="button" class="btn btn-link btn-sm" data-id=${item.id}>删除</button>
              </td>
            </tr>
        `
      })
      tbody.innerHTML = str
    }
    // 调用函数
    getData()
    //添加方法
    form.addEventListener('submit',function(e){
      e.preventDefault() //阻止浏览器默认行为
      // let result = serialize(form)  //username=zs&age=18
      let result = serialize(form,{hash:true}) //获取form数据对象  {username=zs,age=18}
      if(JSON.stringify(result) == '{}') return  //改变的值 和 原来的值  ===
      axios.post('/api/addbook',result).then(({data})=>{
        console.log(data);
        if(data.status=== 201){
          getData()
          form.reset()  //重置表单  reset()  表单的方法  click()  value = ''
        }else{
          alert(data.msg)
        }
      })
    })

    //删除方法
    tbody.addEventListener('click',function(e){
      console.log(e);
      console.log(e.target.tagName); //获取标签
      if(e.target.tagName === 'BUTTON'){
        // alert('点击了')
        console.log(e.target.dataset.id); //固定写法 dataset
        axios.delete('/api/delbook',{params:{id:e.target.dataset.id}}).then(({data})=>{
          console.log(data);
          if(data.status === 200){
            getData()//更新视图
          }
        })
      }
    })

    //< >  &qt &lt &nbsp;
    //安全问题  xss攻击
  </script>
   
</body>

</html>